#!/bin/sh

# directories
FF_VERSION="4.3.1"

SOURCE="ffmpeg-$FF_VERSION"
FAT="FFmpeg-iOS"

SCRATCH="scratch"
# must be an absolute path
INSTALL_PREFIX=`pwd`/"install_prefix"

COMPILE="y"
LIPO="y"

DEPLOYMENT_TARGET="8.0"


CONFIGURE_FLAGS=""

CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-hwaccels --disable-postproc"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-encoders --enable-encoder=mjpeg"
# CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-encoders"
#CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-decoders --enable-decoder=h264,aac,mpeg*"
#CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-mediacodec --enable-decoder=h264_mediacodec,hevc_mediacodec,mpeg4_mediacodec,vp8_mediacodec,vp9_mediacodec" #android
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-videotoolbox --enable-decoder=mpeg4,mpegvideo,acc,gif,h264"
# CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-muxers --enable-muxer=h264,aac,pcm_*,flv,mp4,avi,rtsp,mjpeg"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-muxers"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-demuxers --enable-demuxer=mp4,h264,aac,avi,flv,rtsp,hls,*mpeg*,pcm_*"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-parsers --enable-parser=h264,aac,*jpeg*,mpeg*,gif"
#CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-protocol=sctp,ftp,concat,icecast"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-bsfs --enable-bsf=aac_adtstoasc --enable-bsf=h264_mp4toannexb --enable-bsf=null --enable-bsf=noise"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-filters --enable-avfilter --enable-filter=anull"


# avresample
#CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-avresample"

ARCHS="arm64 armv7" # x86_64 i386"


if [ "$COMPILE" ]
then
	if [ ! `which yasm` ]
	then
		echo 'Yasm not found'
		if [ ! `which brew` ]
		then
			echo 'Homebrew not found. Trying to install...'
                        ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" \
				|| exit 1
		fi
		echo 'Trying to install Yasm...'
		brew install yasm || exit 1
	fi
	if [ ! `which gas-preprocessor.pl` ]
	then
		echo 'gas-preprocessor.pl not found. Trying to install...'
		(curl -L https://github.com/libav/gas-preprocessor/raw/master/gas-preprocessor.pl \
			-o /usr/local/bin/gas-preprocessor.pl \
			&& chmod +x /usr/local/bin/gas-preprocessor.pl) \
			|| exit 1
	fi

	if [ ! -r $SOURCE ]
	then
		echo 'FFmpeg source not found. Trying to download...'
		curl http://www.ffmpeg.org/releases/$SOURCE.tar.bz2 | tar xj \
			|| exit 1
	fi

	CWD=`pwd`
	for ARCH in $ARCHS
	do
		echo "building $ARCH..."
		mkdir -p "$SCRATCH/$ARCH"
		cd "$SCRATCH/$ARCH"

		CFLAGS="-arch $ARCH"
		if [ "$ARCH" = "i386" -o "$ARCH" = "x86_64" ]
		then
		    PLATFORM="iPhoneSimulator"
		    CFLAGS="$CFLAGS -mios-simulator-version-min=$DEPLOYMENT_TARGET"
		else
		    PLATFORM="iPhoneOS"
		    CFLAGS="$CFLAGS -mios-version-min=$DEPLOYMENT_TARGET -fembed-bitcode"
		    if [ "$ARCH" = "arm64" ]
		    then
		        EXPORT="GASPP_FIX_XCODE5=1"
		    fi
		fi

		XCRUN_SDK=`echo $PLATFORM | tr '[:upper:]' '[:lower:]'`
		CC="xcrun -sdk $XCRUN_SDK clang"

		# force "configure" to use "gas-preprocessor.pl" (FFmpeg 3.3)
		if [ "$ARCH" = "arm64" ]
		then
		    AS="gas-preprocessor.pl -arch aarch64 -- $CC"
		else
		    AS="gas-preprocessor.pl -- $CC"
		fi

		CXXFLAGS="$CFLAGS"
		LDFLAGS="$CFLAGS"

		TMPDIR=${TMPDIR/%\/} $CWD/$SOURCE/configure \
		    --target-os=darwin \
		    --arch=$ARCH \
		    --cc="$CC" \
		    --as="$AS" \
			--prefix="$INSTALL_PREFIX/$ARCH" \
			--enable-static \
			--disable-shared \
			--disable-indevs \
			--disable-outdevs \
			--disable-stripping \
			--enable-pic \
    		--enable-nonfree \
			--enable-gpl \
			--enable-small \
			--enable-version3 \
			--enable-cross-compile \
			--disable-optimizations \
			--disable-runtime-cpudetect \
			--disable-doc \
			--disable-debug \
			--disable-programs \
			--disable-ffmpeg \
			--disable-ffprobe \
			--disable-ffplay \
		    $CONFIGURE_FLAGS \
		    --extra-cflags="$CFLAGS" \
		    --extra-ldflags="$LDFLAGS" \
		|| exit 1

		make -j3 install $EXPORT || exit 1
		cd $CWD
	done
fi

if [ "$LIPO" ]
then
	echo "building fat binaries..."
	mkdir -p $FAT/lib
	set - $ARCHS
	CWD=`pwd`
	cd $INSTALL_PREFIX/$1/lib
	for LIB in *.a
	do
		cd $CWD
		echo lipo -create `find $INSTALL_PREFIX -name $LIB` -output $FAT/lib/$LIB 1>&2
		lipo -create `find $INSTALL_PREFIX -name $LIB` -output $FAT/lib/$LIB || exit 1
	done

	cd $CWD
	cp -rf $INSTALL_PREFIX/$1/include $FAT
fi

echo Done
